<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>

        // 数组的深复制
        // function deepCopyArr(arr) {
        //     const newArr = [];
        //     for (let i = 0; i < arr.length; i++) {
        //         // 判断arr[i] 是不是数组
        //         if (Array.isArray(arr[i])) {
        //             const arr2 = deepCopyArr(arr[i]);
        //             newArr.push(arr2);
        //         } else {
        //             newArr.push(arr[i])
        //         }
        //     }
        //     return newArr;
        // }

        function deepCopyArr(arr) {
            const newArr = [];
            for (let key in arr) {
                // 判断arr[i] 是不是数组
                if (Array.isArray(arr[key])) {
                    const arr2 = deepCopyArr(arr[key]);
                    // newArr.push(arr2);
                    newArr[key] = arr2 ;
                } else {
                    // newArr.push(arr[i]);
                    newArr[key] = arr[key];
                }
            }
            return newArr;
        }


        function deepCopyObj(obj) {
            const newObj = {};
            for (let key in obj) {
                // 判断是不是对象
                if (typeof obj[key] === 'object') {
                    const obj2 = deepCopyObj(obj[key]);
                    newObj[key] = obj2;
                } else {
                    newObj[key] = obj[key];
                }
            }
            return newObj
        }



        // 综合数组和对象
        function deepCopy(data) {
            // 首先排除数组和对象以外的数据
            if(typeof data !== 'object')  throw new Error('参数报错'); ;
            // 判断是数组还是对象
            const res = Array.isArray(data) ? [] : {} ;
            // 遍历
            for(let key in data) {
                // if(typeof data[key] === 'object') {
                //     // const res2 = deepCopy(data[key]) ;
                //     // res[key] = res2 ;
                //     res[key] = deepCopy(data[key]) ;
                // } else {
                //     res[key] = data[key] ;
                // }

                res[key] = typeof data[key] === 'object' ? deepCopy(data[key]) : data[key]
            }
            return res ;
        }


        // const arr1 = [1, 2, [3, 4]];
        // const arr2 = deepCopyArr(arr1);
        // arr1[2].push(5);
        // console.log(arr1, arr2);


        // const obj1 = { 
        //     a: 1, 
        //     b: { 
        //         c: 1 
        //     } 
        // };

        // const obj2 = deepCopyObj(obj1) ;

        // obj1.b.d = 3 ;

        // console.log(obj1 , obj2);



        const arr1 = [1, 2, [3, 4]];
        const arr2 = deepCopy(arr1);
        arr1[2].push(5);
        console.log(arr1, arr2);

         const obj1 = { 
            a: 1, 
            b: { 
                c: 1 
            } 
        };

        const obj2 = deepCopy(obj1) ;

        obj1.b.d = 3 ;

        console.log(obj1 , obj2);



        


    </script>

</body>

</html>